<template>
  <div class="goodsinfo-container">
    <transition v-on:before-enter = "beforeEnter"
      v-on:enter = "enter"
      v-on:after-enter = "afterEnter" >
      <div class="ball" v-if="ballFlag" id="ball"></div>
    </transition>
    <!--  -->
    <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
					 <!-- 轮播图-->
           <swiper :lunbotuList="lunbotu" :isfull="false" ></swiper>
					</div>
				</div>
		</div>
		<div class="mui-card">
				<div class="mui-card-header">商品名称</div>
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
            <p class="price">
              市场价：<del>￥{{goodsInfo.market_price}} </del>&nbsp;&nbsp; 销售价：<span class="now-price">￥{{goodsInfo.sell_price}}</span>
            </p>
            <p>购买数量:<number-box  @getcounted="getSelectedCount"  :max="goodsInfo.stock_quantity"></number-box></p>

            <p>
              <mt-button type="primary" size="small">立即购买</mt-button>
              <mt-button type="danger" size="small" @click="addToShopCar">加入购物车</mt-button>
            </p>
					</div>
				</div>
		</div>
      <!--  -->
		<div class="mui-card">
				<div class="mui-card-header">商品参数</div>
				<div class="mui-card-content">
            <div class="mui-card-content-inner">
              <p>商品型号：{{goodsInfo.title}} </p>
              <p>商品库存：{{goodsInfo.stock_quantity}}</p>
              <p>上架时间：{{goodsInfo.add_time}}</p>
            </div>
          </div>
          <div class="mui-card-footer">
            <mt-button type="primary" size="large" class="buy" plain @click="goGoodsDesc(id)">图文介绍</mt-button>
            <mt-button type="danger" size="large" class="buy" @click="goGoodsComments(id)">商品评论</mt-button>
          </div>
      </div>
   </div>

</template>

<script>
// 导入轮播图组件 
import swiper from '../subcomponents/swiper.vue'
// 导入数量盒子组件
import numberBox from '../subcomponents/goodsInfo_numberBox.vue'
export default {

  data () {
    return {
      id: this.$route.params.id,
      lunbotu:[],
      goodsInfo:{},
      ballFlag: false,
      selectcount:1,
    }
  },

  methods: {
    getLunBoTu(){
      this.$http.get("http://www.liulongbin.top:3005/api/getthumimages/"+this.id).then(result=>{
        if(result.body.status == 0){
          // this.lunbotu = result.body.message;
          // 先循环数组的每一项，为item属性添加img属性
          result.body.message.forEach(item=>{
            item.img = item.src
          })
          this.lunbotu = result.body.message;
        }
      })
    },
    getGoodsInfo(){
      this.$http.get("http://www.liulongbin.top:3005/api/goods/getinfo/"+this.id).then(result=>{
        if(result.body.status == 0){
            this.goodsInfo = result.body.message[0];
        }
      })
    },
    goGoodsDesc(id){
      this.$router.push({name: "goodsdesc", params: {id}})
    },
    goGoodsComments(id){
      this.$router.push({name:'goodscomments', params:{id}})
    },
    addToShopCar(){
      this.ballFlag = !this.ballFlag;
      // 拼接处一个商品详情存储到store中
      var goodsinfo={id:this.id, count:this.selectcount,  price:this.goodsInfo.sell_price, selected:true}
      // 调用store中的方法
      this.$store.commit('addToCar', goodsinfo);
    },
    beforeEnter(el){
      el.style.transfrom = "translate(0, 0)";
    },
    enter(el, done) {
      console.log(this.$refs);
      
      // 此处需操作DOM元素获取小球的位置 dom.getBoundingClientRect()可获取小球的坐标
      // 获取小球在页面中的位置
      // const ballPosition = this.$refs.ball.getBoundingClientRect();
      const ballPosition = document.getElementById('ball').getBoundingClientRect();
      //获取徽标在页面中的位置
      const badgePosition = document.getElementById('badge').getBoundingClientRect();
      const xDist = badgePosition.left - ballPosition.left;
      const yDist = badgePosition.top - ballPosition.top;
      el.offsetWidth;
      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all 1s cubic-bezier(0, 0, .25, 1)";
      done();
    },
    afterEnter(el){
      console.log(123)
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count){
    this.selectcount = count;
    // console.log('父组件拿到的值'+this.selectcount);
    
  }
  },
  components: {
    swiper,
    numberBox,
  },
  created () {
    this.getLunBoTu();
    this.getGoodsInfo();
  },

}
</script>

<style lang='less'>
  .goodsinfo-container{
    // background-color: #fff;
    overflow: hidden;
    .now-price{
      color: #e92322;
      font-weight: 700;
    }
    .ball{
    width: 15px;
    height: 15px;
    border-radius: 50%;
    position: absolute;
    z-index: 99;
    background-color: red;
    left: 78px;
    top: 410px;
    // transform: ;
}
  }
  .mui-card-footer{
    display: block;
    .buy{
      margin: 15px 0;
    }
  }
  .mui-card-content-inner{
    img{
      margin: 0 81px;
    }
  }
</style>
